package com.jctrip.hotel.source.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.jctrip.hotel.common.context.BeanContext;
import com.jctrip.hotel.domain.dao.HtOrderSourceRecordDao;
import com.jctrip.hotel.domain.entity.HtOrderSourceRecord;
import com.jctrip.hotel.source.model.Result;
import com.jctrip.hotel.source.model.wehotel.req.*;
import com.jctrip.hotel.source.model.wehotel.resq.*;
import com.jctrip.hotel.source.service.wehotel.WeHotelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Optional;

@Slf4j
@Api(tags = "wehotel")
@RestController
@RequestMapping("/wehotel")
@ApiSupport(order = 10)
public class WehotelController {
    @Resource
    private WeHotelService weHotelService;

    @PostMapping("/getBrands")
    @ApiOperation(value = "获取酒店品牌")
    @ApiOperationSupport(order = 2)
    public Result getBrandList(@RequestParam(value = "appId", required = false) String appId,
                               @RequestParam(value = "key", required = false) String key,
                               @RequestBody GetBrandList getBrandList) {
        GetBrandListResult brandList;
        try {
            brandList = weHotelService.getBrandList(appId, key, getBrandList);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.ok(brandList);
    }

    @PostMapping("/getHotelIdPage")
    @ApiOperation(value = "getHotelIdPage", notes = "获取酒店列表")
    @ApiOperationSupport(order = 1)
    public Result getHotelIdPage(@RequestParam(value = "appId", required = false) String appId,
                                 @RequestParam(value = "key", required = false) String key,
                                 @RequestBody GetHotelIds getHotelIds) {
        GetHotelIdsResult hotelIdPage;
        try {
            hotelIdPage = weHotelService.getHotelIdPage(appId, key, getHotelIds);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.ok(hotelIdPage);
    }

    @PostMapping("/getHotelInfo")
    @ApiOperation(value = "getHotelInfo", notes = "获取酒店详细信息")
    @ApiOperationSupport(order = 2)
    public Result getHotelInfo(@RequestParam(value = "appId", required = false) String appId,
                               @RequestParam(value = "key", required = false) String key,
                               @RequestBody GetHotelInfo getHotelInfo) {
        HotelInfoData hotelInfo;
        try {
            hotelInfo = weHotelService.getHotelInfo(appId, key, getHotelInfo);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.ok(hotelInfo);
    }

    @PostMapping("/getHotelRoomTypeResult")
    @ApiOperation(value = "getHotelRoomTypeResult", notes = "获取酒店房型信息")
    @ApiOperationSupport(order = 3)
    public Result getHotelRoomTypeResult(@RequestParam(value = "appId", required = false) String appId,
                                         @RequestParam(value = "key", required = false) String key,
                                         @RequestBody GetHotelRoomType getHotelRoomType) {
        GetHotelRoomTypeResult hotelRoomTypeResult;
        try {
            hotelRoomTypeResult = weHotelService.getHotelRoomTypeResult(appId, key, getHotelRoomType);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.ok(hotelRoomTypeResult);
    }

    @PostMapping("/getHotelRoomStatusResult")
    @ApiOperation(value = "getHotelRoomStatusResult", notes = "获取酒店房态信息")
    @ApiOperationSupport(order = 4)
    public Result getHotelRoomStatusResult(@RequestParam(value = "appId", required = false) String appId,
                                           @RequestParam(value = "key", required = false) String key,
                                           @RequestBody GetHotelRoomStatus getHotelRoomStatus) {
        GetHotelRoomStatusResult hotelRoomStatusResult;
        try {
            hotelRoomStatusResult = weHotelService.getHotelRoomStatusResult(appId, key, getHotelRoomStatus);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.ok(hotelRoomStatusResult);
    }

    @PostMapping("/getHotelRealRoomStatusResult")
    @ApiOperation(value = "getHotelRealRoomStatusResult", notes = "获取酒店实时房态信息")
    @ApiOperationSupport(order = 5)
    public Result getHotelRealRoomStatusResult(@RequestParam(value = "appId", required = false) String appId,
                                               @RequestParam(value = "key", required = false) String key,
                                               @RequestBody GetHotelRealRoomStatus getHotelRealRoomStatus) {
        GetHotelRoomStatusResult hotelRealRoomStatusResult;
        try {
            hotelRealRoomStatusResult = weHotelService.getHotelRealRoomStatusResult(appId, key, getHotelRealRoomStatus);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.ok(hotelRealRoomStatusResult);
    }


    @PostMapping("/queryOrder")
    @ApiOperation(value = "queryOrder", notes = "查询订单状态信息")
    @ApiOperationSupport(order = 7)
    public Result queryOrder(@RequestParam(value = "appId", required = false) String appId,
                             @RequestParam(value = "key", required = false) String key,
                             @RequestBody QueryOrder queryOrder) {
        OrderData data;
        try {
            data = weHotelService.queryOrder(appId, key, queryOrder);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.ok(data);
    }

    @PostMapping("/cancelOrder")
    @ApiOperation(value = "cancelOrder", notes = "取消订单")
    @ApiOperationSupport(order = 8)
    public Result cancelOrder(@RequestParam(value = "appId", required = false) String appId,
                              @RequestParam(value = "key", required = false) String key,
                              @RequestBody CancelOrder cancelOrder) {
        try {
            weHotelService.cancelOrder(appId, key, cancelOrder);
            return Result.ok();
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    //================接口直连================
    @GetMapping("/queryOrderAll")
    @ApiOperation(value = "queryOrderAll", notes = "接口直连查询订单详细信息")
    public Result queryOrderAll(@RequestParam(value = "sourceId") @ApiParam("供应商账户id") String sourceId,
                                @RequestParam @ApiParam("第三方订单号") String externalId) {
        QueryOrderResp queryOrderResp;
        try {
            QueryOrder queryOrder = QueryOrder.builder()
                    .externalId(externalId.trim())
                    .build();

            HtOrderSourceRecordDao htOrderSourceRecordDao =
                    BeanContext.getApplicationContext().getBean(HtOrderSourceRecordDao.class);
            HtOrderSourceRecord sourceRecord = Optional.ofNullable(htOrderSourceRecordDao.selectOne(new LambdaQueryWrapper<HtOrderSourceRecord>()
                    .eq(HtOrderSourceRecord::getBookId, externalId.trim()))).orElse(new HtOrderSourceRecord());
            queryOrder.setJsonOrderID(sourceRecord.getSourceOrderId());
            queryOrderResp = weHotelService.queryOrderAll(sourceId, queryOrder);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.ok(queryOrderResp);
    }

    @PostMapping("/postOrder2C")
    @ApiOperation(value = "锦江人工下单")
    public Result postOrder2C(@RequestParam String appId,
                              @RequestParam String secret,
                              @RequestBody PostOrder postOrder) {
        OrderData orderData;
        try{
            orderData = weHotelService.postOrder2C(appId, secret, postOrder);
        } catch (Exception e){
            return Result.error(e.getMessage());
        }
        return Result.ok(orderData);
    }

    @GetMapping("/getHotelImage")
    @ApiOperation(value = "获取酒店图片")
    public Result getHotelImage(@RequestParam String appId,
                                @RequestParam String key,
                                @RequestParam String innId) {
        GetHotelImageResult hotelImage;
        try {
            GetHotelImage image = new GetHotelImage();
            image.setInnId(innId);
            hotelImage = weHotelService.getHotelImage(appId, key, image);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.ok(hotelImage);
    }
}
